// Describe:
package udp

import (
	"github.com/spf13/viper"
	"strconv"
	"strings"
	"sync"
	"wormhole/pkg/event"
	"wormhole/protos/hole"
)

func AddNet(targetNet string) {
	sub := strings.Split(targetNet, ".")
	if sub[len(sub)-1] == "255" {
		head := sub[0] + "." + sub[1] + "." + sub[2] + "."
		wg := sync.WaitGroup{}
		for i := 1; i < 255; i++ {
			wg.Add(1)
			go func(p int) {
				ripple := &hole.Ripple{
					Ip:    head + strconv.Itoa(p),
					Port:  viper.GetInt32("core.port"),
					Event: event.BroadcastEvent,
				}
				_ = Dimple(ripple)
				wg.Done()
			}(i)
		}
		wg.Wait()
	} else {
		ripple := &hole.Ripple{
			Ip:    targetNet,
			Port:  viper.GetInt32("core.port"),
			Event: event.BroadcastEvent,
		}
		_ = Dimple(ripple)
	}
}
